package cn.btks.resultintegration.achievements.repository;

import cn.btks.resultintegration.achievements.pojo.Achievements;
import cn.btks.resultintegration.achievements.pojo.AchievementsFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

/**
 * @Author lijun
 * @Date 2021/4/19 9:26
 */
@Repository
public class BaseAchievementsFileRepository {

    @Autowired
    JdbcTemplate jdbcTemplate;

    /**
     * 更新文件为历史数据
     * @param achievementsFileList
     */
    public void updateRoadSegment(List<AchievementsFile> achievementsFileList) {
        String sql = "update achievements_file set is_history = ? where achievements_id = ?";
        jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement preparedStatement, int i) throws SQLException {
                AchievementsFile achievementsFile = achievementsFileList.get(i);
                preparedStatement.setInt(1, 1);
                preparedStatement.setLong(2, achievementsFile.getAchievements().getId());
            }
            @Override
            public int getBatchSize() {
                return achievementsFileList.size();
            }
        });
    }
}
